﻿using System;
using System.Collections.Generic;
using System.Linq;
using Interfaces.FilterModels;
using Interfaces.Filters;
using Interfaces.Models;

namespace BusinessLogic.Models.Filter.GenericFilters
{
    public class NameSupportedFilter<TNameSupported,TFilterModel> : IFilter
        where TNameSupported : INameSupported
        where TFilterModel : INameSupportedFilterModel
    {
        private readonly TFilterModel _filterModel;
        
        public NameSupportedFilter(TFilterModel filterModel)
        {
            _filterModel = filterModel;
        }

        public virtual IEnumerable<object> Filtrate(IEnumerable<object> source)
        {
            if (source != null)
            {
                var target = source as IEnumerable<TNameSupported>;

                if (target == null)
                    throw new ArgumentException("Source has incorrect type.");

                return target.Where(GetNameFilter()) as IEnumerable<object>;
            }
            return null;
        }

        protected virtual Func<TNameSupported, bool> GetNameFilter()
        {
            return item => item.Name.Contains(_filterModel.Name);
        }

        
    }
}
